package com.william.jdbcplus.core.toolkit;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;

public class RedisAtomicLongUtils {
	/** 
	* @Title: set 
	* @Description: set cache.
	* @param key
	* @param value
	* @param expireTime      
	*/
	public static void set(String key, int value, Date expireTime, RedisTemplate<String, Long> redisTemplate) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate);
		counter.set(value);
		counter.expireAt(expireTime);
	}

	/** 
	* @Title: set 
	* @Description: set cache.
	* @param key
	* @param value
	* @param timeout
	* @param unit      
	*/
	public static void set(String key, int value, long timeout, TimeUnit unit, RedisTemplate<String, Long> redisTemplate) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate);
		counter.set(value);
		counter.expire(timeout, unit);
	}

	/** 
	* @Title: generate 
	* @Description: Atomically increments by one the current value.
	* @param key
	* @return      
	*/
	public static long generate(String key, RedisTemplate<String, Long> redisTemplate) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate);
		return counter.incrementAndGet();
	}

	/** 
	* @Title: generate 
	* @Description: Atomically increments by one the current value.
	* @param key
	* @return      
	*/
	public static long generate(String key, Date expireTime, RedisTemplate<String, Long> redisTemplate) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate);
		counter.expireAt(expireTime);
		return counter.incrementAndGet();
	}

	/** 
	* @Title: generate 
	* @Description: Atomically adds the given value to the current value.
	* @param key
	* @param increment
	* @return      
	*/
	public static long generate(String key, int increment, RedisTemplate<String, Long> redisTemplate) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate);
		return counter.addAndGet(increment);
	}

	/** 
	* @Title: generate 
	* @Description: Atomically adds the given value to the current value.
	* @param key
	* @param increment
	* @param expireTime
	* @return      
	*/
	public static long generate(String key, int increment, Date expireTime, RedisTemplate<String, Long> redisTemplate) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate);
		counter.expireAt(expireTime);
		return counter.addAndGet(increment);
	}
}
